﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.WowAddonStudio.FrameXml.Components.Actions;
using Microsoft.WowAddonStudio.FrameXml.Components.Designers;
using Microsoft.WowAddonStudio.FrameXml.Components.Support;
using Microsoft.WowAddonStudio.FrameXml.Serialization;
using Microsoft.WowAddonStudio.FrameXml.Serialization.LayoutFrames;
using Action=Microsoft.WowAddonStudio.FrameXml.Components.Actions.Action;

namespace Microsoft.WowAddonStudio.FrameXml.Components
{
	[ToolboxBitmap(typeof (PictureBox), "PictureBox.bmp")]
	[ToolboxItemFilter("WowAddonStudio", ToolboxItemFilterType.Require)]
    [Designer(typeof(LayoutFrameContainerDesigner))]
    public class TextureControl : LayoutFrameControl
	{
		/// <summary>
		/// Initializes a new instance of the <see cref="TextureControl"/> class.
		/// </summary>
		public TextureControl()
		{

		}

		/// <summary>
		/// Initializes a new instance of the <see cref="TextureControl"/> class.
		/// </summary>
		/// <param name="texture">The texture.</param>
		public TextureControl(Texture texture)
			: base(texture)
		{
		}

		/// <summary>
		/// Gets the texture.
		/// </summary>
		public Texture Texture
		{
			get { return SerializationObject as Texture; }
		}

		/// <summary>
		/// Gets the type of the serialization object this control manages.
		/// </summary>
		/// <value></value>
		public override Type SerializationObjectType
		{
			get { return typeof (Texture); }
		}

		/// <summary>
		/// Gets or sets the size that is the lower limit that <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)"/> can specify.
		/// </summary>
		/// <value></value>
		/// <returns>An ordered pair of type <see cref="T:System.Drawing.Size"/> representing the width and height of a rectangle.</returns>
		public override Size MinimumSize
		{
			get { return new Size(16, 16); }
			set { base.MinimumSize = value; }
		}

        /// <summary>
        /// Gets the default size of a FrameXML control.
        /// </summary>
        /// <value></value>
        protected override Size DefaultSize
        {
            get { return new Size(64, 64); }
        }

	    /// <summary>
		/// Draws the FrameXML control.
		/// </summary>
		/// <param name="graphics">The <see cref="FrameXmlGraphics"/> context to draw to.</param>
		/// <param name="rectangle">The <see cref="Rectangle"/> to draw to.</param>
		public override bool DrawControl(FrameXmlGraphics graphics, Rectangle rectangle)
		{
		    return graphics.DrawTexture(Texture, rectangle);
		}

        /// <summary>
        /// Gets the actions for the control.
        /// </summary>
        /// <returns>
        /// An array of <see cref="Action"/> instances.
        /// </returns>
        public override Action[] GetActions()
        {
            if(GetType() == typeof(TextureControl))
            {
                return new Action[]
                   {
                       new SetDefaultValueAction(this, "TexCoords")
                           {
                               Category = "Create",
                               DefaultValue = new TexCoords{Left = 0, Top = 0, Right = 1, Bottom = 1}
                           }
                   };
            }

            return new Action[0];
        }
	}
}